import { createWebHistory, createRouter } from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import Home from './views/Home.vue'
import Login from './views/Login.vue'
import { useHomeStore } from '@/store'

const routes = [
  { path: '/', component: Home },
  { path: '/login', component: Login },
  { path: '/no-perm', component: () => import('./views/NoPerm.vue') },
  { path: '/:path(.*)', component: () => import('./views/NotFound.vue') }
]

const router = createRouter({ history: createWebHistory(import.meta.env.VITE_CONTEXT_PATH), routes })
router.beforeEach((to, from, next) => {
  const homeStore = useHomeStore()
  // 登录检查
  if (to.path != '/login' && document.cookie.indexOf('Authorization') == -1) {
    location.href = import.meta.env.VITE_CONTEXT_PATH + '/login'
  } else {
    next()
  }
})

/**
 * 进度条状态设置
 */
router.beforeEach((to, from, next) => {
  NProgress.start() // 开始进度条
  next()
})

router.afterEach(() => {
  NProgress.done() // 结束进度条
})

export default router
